# -*-coding:utf-8 -*-
"""

"""
import re
import utiltool
import config as conf


def check_params(correct_params):
    """
    判断是否缺少参数
    :param correct_params:
    :return:
    """
    def _check_params(fn):
        def _deco(request_handler):
            query_keys = request_handler.request.arguments.keys()
            for c_p_item in correct_params:
                if c_p_item not in query_keys:
                    return utiltool.response_write(request_handler, {"code": "1002", "msg": conf.CODE1002})
            return fn(request_handler)
        return _deco
    return _check_params


def verify_params(fn):
    """
    参数格式验证
    :param fn:
    :return:
    """
    def _check(request_handler):
        for key, value in request_handler.request.arguments.iteritems():
            if key in conf.dict_param_re.iterkeys():
                if not re.compile(conf.dict_param_re[key]).match(value[0]):
                    return utiltool.response_write(request_handler, {"code": "1002", "msg": conf.CODE1002})
        return fn(request_handler)
    return _check
